﻿// Copyright 2009 Mike Geise
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
//     http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

namespace Falcon.Core.Controllers
{
	using System.Web.Mvc;
	using Falcon.Core.Domain;
	using Falcon.Core.Domain.Services;
	using Falcon.Core.Domain.Values;
	using Falcon.Core.Extensions;
	using Falcon.Core.Validation;

	[Authorize]
	public class AttachmentRuleController : DefaultController
	{
		public AttachmentRuleController(IAttachmentRuleService attachmentRuleService)
		{
			this.AttachmentRuleService = attachmentRuleService;
		}

		public IAttachmentRuleService AttachmentRuleService
		{
			get;
			private set;
		}

		[AcceptVerbs(HttpVerbs.Get)]
		public ActionResult Index(int? page)
		{
			var rules = AttachmentRuleService.GetPaged(new AttachmentRuleSpecification
			{
				Page = page,
				Limit = Settings.Attachments.RulePageLimit
			});

			var rule = rules.FirstOrDefault();

			return rule.CanView ? View(Views.Index, rules) : NotAuthorized();
		}

		[AcceptVerbs(HttpVerbs.Get)]
		public ActionResult Create()
		{
			var rule = AttachmentRuleService.Create();

			return rule.CanCreate ? View(Views.Create, new AttachmentRuleCreate()) : NotAuthorized();
		}

		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult Create(AttachmentRuleCreate value)
		{
			try
			{
				var rule = AttachmentRuleService.Create();

				if (!rule.CanCreate)
				{
					return NotAuthorized();
				}

				this.AttachmentRuleService.Insert(rule, value);

				return Message(Routes.AttachmentRuleUpdate, new { id = rule.Id }, Resources.Messages.AttachmentRuleCreated);
			}
			catch (ValidationException exception)
			{
				ModelState.CopyValidationExceptions(exception);
			}

			return View(Views.Create, value);
		}

		[AcceptVerbs(HttpVerbs.Get)]
		public ActionResult Update(int id)
		{
			var rule = AttachmentRuleService.GetById(id);

			if (rule == null)
			{
				return Error(Resources.Messages.AttachmentRuleNotFound);
			}

			return rule.CanUpdate ? View(Views.Update, new AttachmentRuleUpdate(rule)) : NotAuthorized();
		}

		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult Update(AttachmentRuleUpdate value)
		{
			try
			{
				var rule = AttachmentRuleService.GetById(value.Id);

				if (rule == null)
				{
					return Error(Resources.Messages.AttachmentRuleNotFound);
				}

				if (!rule.CanUpdate)
				{
					return NotAuthorized();
				}

				this.AttachmentRuleService.Update(rule, value);

				return Message(Routes.AttachmentRuleUpdate, new { id = rule.Id }, Resources.Messages.AttachmentRuleUpdated);
			}
			catch (ValidationException exception)
			{
				ModelState.CopyValidationExceptions(exception);
			}

			return View(Views.Update, value);
		}

		[AcceptVerbs(HttpVerbs.Get)]
		public ActionResult Delete(int id)
		{
			var rule = this.AttachmentRuleService.GetById(id);

			if (rule == null)
			{
				return base.Error(Resources.Messages.AttachmentRuleNotFound);
			}

			return rule.CanDelete ? base.View(Views.Delete, new AttachmentRuleDelete(rule)) : NotAuthorized();
		}

		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult Delete(AttachmentRuleDelete value)
		{
			try
			{
				var rule = this.AttachmentRuleService.GetById(value.Id);

				if (rule == null)
				{
					return base.Error(Resources.Messages.AttachmentRuleNotFound);
				}

				if (!rule.CanDelete)
				{
					return NotAuthorized();
				}

				this.AttachmentRuleService.Delete(rule);

				return base.RedirectToRoute(Routes.AttachmentRuleIndex);
			}
			catch (ValidationException exception)
			{
				base.ModelState.CopyValidationExceptions(exception);
			}

			return base.View(Views.Delete, value);
		}
	}
}